<?php

namespace addons\qingdongams\model;

use think\Db;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;

/**
 *客户产品
 */
class CustomerProduct Extends Model {
	use SoftDelete;

    // 表名,不含前缀
    protected $name = 'qingdongams_customer_product';
	// 开启自动写入时间戳字段
	protected $autoWriteTimestamp = 'int';
	// 定义时间戳字段名
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';
	protected $deleteTime = 'deletetime';
    // 最后一个工单idw
    protected $append = [
        'last_workorder_id',
        'file_ids_data',
    ];
    public function getLastWorkorderIdAttr(){
        return Workorder::where(['customer_product_id'=>$this->id])->order('id desc')->value('id')??0;
    }
    //
    public function getFileIdsDataAttr($value,$data) {
        $files  = explode(',', $data['file_ids']??'');
        $result = [];
        foreach ($files as $fid) {
            if ($fid) {
                $result[] = ['id'=>$fid,'url'=>cdnurl(File::getUrl($fid), true)];
            }
        }

        return $result;
    }



    //获取创建时间
	public function getCreatetimeAttr($value) {
		return date('Y-m-d H:i:s', $value);
	}
    //
    public function getFileIdsAttr($value) {
        $files  = explode(',', $value);
        $result = [];
        foreach ($files as $fid) {
            if ($fid) {
                $result[] = cdnurl(File::getUrl($fid), true);
            }
        }

        return $result;
    }


	//创建人
	public function createStaff() {
		return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name');
	}

	//客户
	public function customer() {
		return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow,address');
	}
	//产品
	public function product() {
		return $this->hasOne(Product::class, 'id', 'product_id')->with('productType')->field('id,name,unit,num,img,price,description,type_id');
	}
    //订单
    public function contracts() {
        return $this->hasOne(Contract::class, 'id', 'contract_id')->field('id,name,num,money,order_date,end_time,start_time');
    }
    public function equipment() {
        return $this->hasOne(Equipment::class, 'number', 'number')->field('id,number,logo');
    }
	//创建客户
	public static function createProduct($params) {

		$staff = Staff::info();
		if(!empty($staff)){
			$params['create_staff_id'] = $staff->id;
		}

		$customer              = new self;
		// 调用当前模型对应的User验证器类进行数据验证
		$result = $customer->allowField(true)->save($params);
		$lastId=$customer->getLastInsID();
		if (false === $result) {
			// 验证失败 输出错误信息
			throw new Exception($customer->getError());
		}

		return $lastId;
	}

	public function getStatusAttr($value){
	    $list = [0=>'待发货',1=>'发货中',2=>'待安装',3=>'安装成功',4=>'旧机维修',9=>'已删除'];
	    return $list[$value]??'';
    }

	/**
	 *修改客户信息
	 */
	public static function updateProduct($params) {

		$customer = new self;
		// 调用当前模型对应的User验证器类进行数据验证
		$result = $customer->save($params, ['id' => $params['id']]);
		if (false === $result) {
			// 验证失败 输出错误信息
			throw new Exception($customer->getError());
		}

		return true;
	}
    //是否可以修改产品编号
	public static function isEditNumber($id){
        $product=CustomerProduct::where(['id'=>$id,'status'=>['in',[0,1]]])->find();
        if(empty($product)){
            return false;
        }
        return true;
    }

}
